<template>
  <div class="home">
    <el-row :gutter="20">
      <el-col :span="8">
        <div class="day-oil">
          <div class="top">
            <p class="grayText">今日加油</p>
            <p style="margin-top: 13px">
              <span class="bigText">{{ todayFuelTotal.vol }}L</span>
              <span class="font-18-text" style="margin-left: 9px">/{{ todayFuelTotal.cnt }}笔</span>
            </p>
          </div>
          <div id="oil-chart" style="width: 100%; height: 104px"></div>
        </div>
      </el-col>
      <el-col :span="10">
        <div>
          <div class="flex">
            <div class="card-box-index flex-1">
              <p class="flex">
                <span>子卡数量</span>
                <span class="el-icon-arrow-right"></span>
              </p>
              <p class="bigText" style="margin-top: 14px">{{ otherData.memberCnt }}</p>
            </div>
            <!-- <div class="card-box-index flex-1" style="margin-left: 16px;">
              <p class="flex" @click="toMember">
                <span>散户总数</span>
                <span class="el-icon-arrow-right"></span>
              </p>
              <p class="bigText" style="margin-top: 14px">{{ otherData.memberCnt }}</p>
            </div> -->
          </div>
          <div class="flex" style="margin-top: 16px">
            <div class="card-box-index flex-1">
              <p class="flex">
                <span>客户余额</span>
                <!-- <span class="el-icon-arrow-right"></span> -->
              </p>
              <p class="bigText" style="margin-top: 14px">{{ otherData.accAmt }}元</p>
            </div>
            <div class="card-box-index flex-1" style="margin-left: 16px;">
              <p class="flex">
                <span>已透支金额</span>
                <!-- <span class="el-icon-arrow-right"></span> -->
              </p>
              <p class="bigText" style="margin-top: 14px">{{ otherData.accCredit }}元</p>
            </div>
          </div>
        </div>
      </el-col>
      <!--<el-col :span="8">
        <div class="card-box-index message-box">
          <p>消息</p>
          <ul class="message">
             <li class="flex">
              <p class="phrase">Ant Design Pro 4.0 正式发布</p>
              <p>1周前</p>
            </li>
          </ul>
        </div>
      </el-col>-->

      <el-col :span="24" style="margin-top: 20px">
        <div class="card-box-index bar-line-box">
          <div style="margin-bottom: 20px">
            <p class="card-title">近7日加油情况</p>
          </div>
          <div id="bar-line" style="width: 100%;height: 210px"></div>
        </div>
      </el-col>
    </el-row>
    <!-- <el-divider /> -->
  </div>
</template>

<script>
  import {
    overview
  } from '@/api/fleet';
  import * as echarts from 'echarts';
  import {
    formatCurrency
  } from '../utils';
  export default {
    name: "Index",
    components: {

    },
    data() {
      return {
        version: "4.8.2",
        dayFuelData: [],
        otherData: {},
        todayFuelTotal: {},
        lastDayFuel: [],
        tankData: [],
        prList: [],
        stationList: []
      };
    },
    async mounted() {
      await this.getHomeData()
      this.initDayOilLineChart();
      this.initSevenDayBarLine();
      // await this.getStationAndPr()
      // this.getHomeWarnData()
    },
    methods: {
      async getHomeData() {
        await overview().then(res => {
          if (res.code === 200) {
            this.dayFuelData = res.data.todayFuelHour;
            this.otherData = {
              memberCnt: res.data.memberCnt,
              accAmt: formatCurrency(res.data.accAmt),
              accCredit: formatCurrency(Math.abs(res.data.accCredit))
            };
            this.todayFuelTotal = res.data.todayFuelTotal;
            this.lastDayFuel = res.data.lastDayFuel
          }
        })
      },
      goTarget(href) {
        window.open(href, "_blank");
      },
      initDayOilLineChart() {
        const myChart = echarts.init(document.getElementById('oil-chart'));
        const dataArr = this.dayFuelData.map(item => {
          return {
            name: item.key,
            value: item.cnt
          }
          // return item.cnt
        })
        const xAxis = this.dayFuelData.map(item => item.key)
        const option = {
          legend: {
            icon: "rect",
            itemHeight: 1,
            itemWidth: 10,
            left: 0
          },
          grid: {
            left: 0,
            width: '100%',
            right: 0,
            top: 0,
            bottom: 30
          },
          xAxis: {
            type: 'category',
            splitLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLine: {
              show: false
            },
            boundaryGap: false,
            // data: xAxis,
            axisLabel: {
              formatter: function(value, index) {
                //console.log(value)
                if (index % 4 === 0) {
                  return xAxis[index]

                }
              },
              padding: [2, 0, 2, 32],
              color: '#1B2232'
            }
          },
          yAxis: {
            splitLine: {
              show: false
            },
            axisLine: {
              show: false
            },
            axisLabel: {
              show: false
            }
          },
          series: [{
            name: '今日',
            type: 'line',
            hoverAnimation: false,
            // smooth: true,
            showSymbol: false,
            data: dataArr,
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                  offset: 0,
                  color: 'rgba(58,77,233,0.8)'
                },
                {
                  offset: 1,
                  color: 'rgba(58,77,233,0.3)'
                }
              ])
            },
          }]
        }
        myChart.setOption(option)
        window.addEventListener('resize', function() {
          myChart.resize();
        });
      },
      initSevenDayBarLine() {
        const data1 = this.lastDayFuel ? this.lastDayFuel.map(item => item.vol) : [];
        const xAxis = this.lastDayFuel ? this.lastDayFuel.map(item => item.key) : []
        const myChart = echarts.init(document.getElementById('bar-line'));
        const option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              crossStyle: {
                color: '#999'
              }
            },
          },
          grid: {
            width: '94%',
            top: 40,
            bottom: 20,
            left: 40,
            right: 50
          },
          legend: {
            data: ['加油'],
            left: 5
          },
          xAxis: [{
            type: 'category',
            data: xAxis,
            axisPointer: {
              type: 'shadow'
            }
          }],
          yAxis: [{
              type: 'value',
              min: 0,
              // max: 2000,
              // interval: 500,
              axisLabel: {
                formatter: '{value}'
              }
            },
            // {
            //   type: 'value',
            //   min: 0,
            //   max: 100,
            //   interval: 25,
            //   axisLabel: {
            //     formatter: '{value} %'
            //   }
            // }
          ],
          series: [{
              name: '加油',
              type: 'bar',
              tooltip: {
                valueFormatter: function(value) {
                  return value;
                }
              },
              color: '#448FFF',
              data: data1,
              barMinWidth: 20,
              barMaxWidth: 60
            },
            // {
            //   name: '同比上涨（%）',
            //   type: 'line',
            //   yAxisIndex: 1,
            //   tooltip: {
            //     valueFormatter: function (value) {
            //       return value + '%';
            //     }
            //   },
            //   color: '#61D7A7',
            //   data: [],
            //   label: {
            //     show: true,
            //     position: 'top'
            //   },
            // }
          ]
        }
        myChart.setOption(option)
        window.addEventListener('resize', function() {
          myChart.resize();
        })
      },
      toCompany() {
        this.$router.push({
          path: '/member/company'
        })
      },
      toMember() {
        this.$router.push({
          path: '/member/subcard'
        })
      }
    },
  };
</script>

<style scoped lang="scss">
  .home {
    .day-oil {
      background: #FFFFFF;
      box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.06);
      border-radius: 2px;

      .top {
        padding: 16px 16px 0;
        margin-bottom: 24px;
      }
    }

    .card-box-index {
      background: #FFFFFF;
      box-shadow: 0px 1px 4px 0px rgba(0, 0, 0, 0.06);
      border-radius: 2px;
      padding: 16px;
      box-sizing: border-box;
    }

    .grayText {
      font-family: PingFangSC, PingFang SC;
      font-weight: 500;
      font-size: 16px;
      color: #000000;
      line-height: 16px;
    }

    .bigText {
      font-family: HelveticaNeue;
      font-size: 24px;
      color: rgba(0, 0, 0, 0.85);
      line-height: 24px;
    }

    .font-18-text {
      font-family: PingFangSC, PingFang SC;
      font-weight: 400;
      font-size: 18px;
      color: #000000;
      line-height: 18px;
    }

    .message-box {
      height: 197px;
    }

    .message {
      margin-top: 11px;

      li {
        font-family: PingFangSC, PingFang SC;
        font-weight: 400;
        font-size: 14px;
        color: #000000;
        justify-content: space-between;
      }

      .phrase {
        width: 198px;
        overflow: hidden;
        word-break: keep-all;
        text-overflow: ellipsis;
      }
    }

    .bar-line-box {
      .card-title {
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        font-size: 16px;
        color: #000000;
        line-height: 16px;
      }
    }

    .stock {
      .card-title {
        font-family: PingFangSC, PingFang SC;
        font-weight: 500;
        font-size: 16px;
        color: rgba(0, 0, 0, 0.85);
        line-height: 28px;
      }
    }
  }

  .flex {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
  }

  .font-size-16 {
    color: 16px;
  }

  .font-size-12 {
    color: 112px;
  }

  .box-sizing-border {
    box-sizing: border-box;
  }

  .flex-1 {
    flex: 1;
  }
</style>
